﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using SES.CMS.DO;
using SES.CMS.BL;

namespace SES.CMS
{
    public partial class LibsFile : System.Web.UI.Page
    {
        cmsLibsFileDO objCat = new cmsLibsFileDO();
        int pageSize = 10;
        protected void Page_Load(object sender, EventArgs e)
        {
            MasterPage mt = this.Master as MasterPage;
            Control uc = mt.FindControl("ucHomeSlide1") as Control;
            uc.Visible = false;
            if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
            {
                int ID = int.Parse(Request.QueryString["ID"]);
                string Title = new cmsCategoryBL().Select(new cmsCategoryDO { CategoryID = ID }).Title;
                Page.Title = Title + " - " + Ultility.PageTitle();
                HtmlMeta metaDesc = new HtmlMeta();
                metaDesc.Name = "description";
                metaDesc.Content = Title + " - " + new sysConfigBL().Select(new sysConfigDO { ConfigID = 1 }).ConfigValue;
                Page.Header.Controls.Add(metaDesc);
                lblTitle.Text = Title;
            }
            else
            {
                Page.Title = Ultility.PageTitle();
                HtmlMeta metaDesc = new HtmlMeta();
                metaDesc.Name = "description";
                metaDesc.Content = new sysConfigBL().Select(new sysConfigDO { ConfigID = 1 }).ConfigValue;
                Page.Header.Controls.Add(metaDesc);
                lblTitle.Text = "";
            }
            if (!IsPostBack)
            {
                hdfPage.Value = "1";
                BindrptOtherNews();
            }
        }

        protected string FriendlyUrl(string strTitle)
        {
            return Ultility.Change_AV(strTitle);
        }

        public void BindrptOtherNews()
        {
            DataTable dtConten = new DataTable("dtConten");
            int page = 0;
            page = int.Parse(hdfPage.Value.ToString());
            dtConten = new cmsLibsFileBL().SelectLibsFile_GetByPage(page, pageSize);
            if ((dtConten.Rows.Count > 0) && (dtConten != null))
            {
                int pageMax = 0;
                int countRow = 0;
                rptModuleTopCate.DataSource = dtConten;
                rptModuleTopCate.DataBind();
                if (dtConten.Rows[0]["CountRow"] != null)
                {
                    countRow = int.Parse(dtConten.Rows[0]["CountRow"].ToString());
                }
                if (countRow % pageSize == 0)
                {
                    pageMax = countRow / pageSize;
                }
                else
                {
                    pageMax = (countRow / pageSize) + 1;
                }
                if ((page == 1) && (page != pageMax))
                {
                    PreviousPage.Visible = false;
                    NextPage.Visible = true;
                }
                else if ((page != 1) && (page == pageMax))
                {
                    PreviousPage.Visible = true;
                    NextPage.Visible = false;
                }
                else if ((page == 1) && (page == pageMax))
                {
                    PreviousPage.Visible = false;
                    NextPage.Visible = false;
                }
                else
                {
                    PreviousPage.Visible = true;
                    NextPage.Visible = true;
                }
            }
            else
            {
                lblTitle.Text = "Không có nội dung trong chuyên mục này!";
                PreviousPage.Visible = false;
                NextPage.Visible = false;
            }
        }

        protected void NextPage_Click(object sender, EventArgs e)
        {
            hdfPage.Value = Convert.ToString(int.Parse(hdfPage.Value.ToString()) + 1);
            this.BindrptOtherNews();
        }

        protected void PreviousPage_Click(object sender, EventArgs e)
        {
            if (hdfPage.Value != "1")
            {
                hdfPage.Value = Convert.ToString(int.Parse(hdfPage.Value.ToString()) - 1);
            }
            this.BindrptOtherNews();
        }

        public string FriendlyUrl2(string s)
        {
            return Ultility.Change_AV2(s);
        }

        public string FileImg(string FileType)
        {
            string sReturn = "";
            switch (FileType)
            {
                case ".doc":
                    sReturn = "/Images/doc.png";
                    break;
                case ".docx":
                    sReturn = "/Images/doc.png";
                    break;
                case ".xls":
                    sReturn = "/Images/xls.png";
                    break;
                case ".xlsx":
                    sReturn = "/Images/xls.png";
                    break;
                case ".ppt":
                    sReturn = "/Images/ppt.png";
                    break;
                case ".pptx":
                    sReturn = "/Images/ppt.png";
                    break;
                case ".rar":
                    sReturn = "/Images/rar.png";
                    break;
                case ".pdf":
                    sReturn = "/Images/pdf.png";
                    break;
                case ".chm":
                    sReturn = "/Images/chm.png";
                    break;
                case ".jpg":
                    sReturn = "/Images/FileImage.png";
                    break;
                case ".png":
                    sReturn = "/Images/FileImage.png";
                    break;
                case ".psd":
                    sReturn = "/Images/FileImage.png";
                    break;
                case ".gif":
                    sReturn = "/Images/FileImage.png";
                    break;
                case ".mp3":
                    sReturn = "/Images/Audio.png";
                    break;
                case ".mp4":
                    sReturn = "/Images/Audio.png";
                    break;
                case ".avi":
                    sReturn = "/Images/Audio.png";
                    break;
                case ".wmv":
                    sReturn = "/Images/Audio.png";
                    break;
                case ".mkv":
                    sReturn = "/Images/Audio.png";
                    break;
                default:
                    sReturn = "/Images/NotFile.png";
                    break;
            }
            return sReturn;
        }
    }
}